/** Manager class to control and handle our GPS engine **/

package com.syde322;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.syde322.LocationApp.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.*;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class LocationProcessor implements LocationListener {

	// Members
	private static LocationManager loc_manager;
	
	private Context context;
	private boolean GPS_ENABLED = false;
	private boolean NET_ENABLED = false;
	private static Location gps_loc;
	
	LocationProcessor(Context c) {
		this.context = c;
	}
	
	// Must call this within the Activity's init()
	public void init() {
		LocationProcessor.loc_manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
	}
	
	// Accessors
	public LocationManager getLocationManager() {
		return LocationProcessor.loc_manager;
	}
	
	public static Location getLocation() {
		return gps_loc;
	}
	
	
	public String getBestProvider() {
		//Try GPS first, then Network
		if(GPSEnabled()) {
			return LocationManager.GPS_PROVIDER;
		}
		else if(netEnabled()) {
			return LocationManager.NETWORK_PROVIDER;
		} 
		return null;
	}

	public boolean GPSEnabled()
	{
		try {
			this.GPS_ENABLED = LocationProcessor.loc_manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return this.GPS_ENABLED;
	}
	
	public boolean netEnabled()
	{
		try {
			this.NET_ENABLED = LocationProcessor.loc_manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return this.NET_ENABLED;
	}
	
	public void getLocationForProcessing() {
		String provider = this.getBestProvider();
		if(provider!=null){
			gps_loc = LocationProcessor.loc_manager.getLastKnownLocation(provider);
		}
		// If GPS fails, prompt input
		if(gps_loc == null){
			LocationProcessor.gps_loc = new Location(provider);
			
			LocationProcessor.gps_loc.setLatitude(43.464258);
			LocationProcessor.gps_loc.setLongitude(-80.520410);
			
			/*
			LayoutInflater factory = LayoutInflater.from(this.context);            
	        final View textEntryView = factory.inflate(R.layout.geolayout, null);
			
			AlertDialog.Builder alert = new AlertDialog.Builder(this.context);
			alert.setTitle("Enter Coordinates:");
			alert.setMessage("GPS is currently not available.");
			alert.setView(textEntryView);
			AlertDialog prompt = alert.create();
			
			final EditText latitude = (EditText)textEntryView.findViewById(R.id.editText1);
			final EditText longitude = (EditText)textEntryView.findViewById(R.id.editText2);
			
			alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				String l1 = latitude.getText().toString();
				String l2 = longitude.getText().toString();
				// Set this.gps_loc
				LocationProcessor.gps_loc.setLatitude(Double.valueOf(l1));
				LocationProcessor.gps_loc.setLongitude(Double.valueOf(l2));
			}
			});
			
			alert.show();
			*/
		}
	}
	
	public String getNearestAddress(Location l){
		String nearestAddress = null;
		if(l != null) {
			Geocoder gc = new Geocoder(this.context, Locale.getDefault());
			if(Geocoder.isPresent()){
				try {
				  List<Address> list = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
				  Address a = list.get(0);
				  nearestAddress += a.getLocality();
				  nearestAddress += a.getCountryName();
				} catch (IOException e) {
					e.printStackTrace();
				}				  
			}
		}
		return nearestAddress;
	}
	
	// @Interface LocationListener
	// TODO: @ANSELM - add your functionality for Google Maps in these 
	@Override
	public void onLocationChanged(Location arg0) {}

	@Override
	public void onProviderDisabled(String provider) {}

	@Override
	public void onProviderEnabled(String provider) {}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {}

};
